home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 November / Freeware November 1998.img / dist / fw_bind.idb / usr / freeware / bin / named-bootconf.pl.z / named-bootconf.pl
Perl Script  |  1998-05-26  |  9KB  |  325 lines

  1. #!/usr/bin/perl
  2.  
  3. ## Copyright (c) 1996, 1997 by Internet Software Consortium
  4. ##
  5. ## Permission to use, copy, modify, and distribute this software for any
  6. ## purpose with or without fee is hereby granted, provided that the above
  7. ## copyright notice and this permission notice appear in all copies.
  8. ##
  9. ## THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  10. ## ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  11. ## OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  12. ## CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  13. ## DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  14. ## PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  15. ## ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  16. ## SOFTWARE.
  17.  
  18. ## $Id: named-bootconf.pl,v 8.16 1998/02/13 19:48:25 halley Exp $
  19.  
  20. # This is a filter.  Input is a named.boot.  Output is a named.conf.
  21.  
  22. $new_config = "";
  23.  
  24. $have_options = 0;
  25. %options = ();
  26. %options_comments = ();
  27. @topology = ();
  28. @topology_comments = ();
  29. @bogus = ();
  30. @bogus_comments = ();
  31. @transfer_acl = ();
  32. @transfer_comments = ();
  33. $logging = "";
  34.  
  35. while(<>) {
  36.     next if /^$/;
  37.  
  38.     # skip comment-only lines
  39.     if (/^\s*;+\s*(.*)$/) {
  40.     $new_config .= "// $1\n";
  41.     next;
  42.     }
  43.  
  44.     # handle continued lines
  45.     while (/\\$/) {
  46.     s/\\$/ /;
  47.         $_ .= <>;
  48.     }
  49.     
  50.     chop;
  51.     
  52.     # deal with lines ending in a coment
  53.     if (s/\s*;+\s*(.*)$//) {
  54.     $comment = "// $1";
  55.     } else {
  56.     $comment = "";
  57.     }
  58.  
  59.     ($directive, @rest) = split;
  60.  
  61.     $class = "";
  62.     if ($directive =~ /^(.*)\/(.*)$/) {
  63.     $directive = $1;
  64.     $class = $2;
  65.     }
  66.     
  67.     if ($directive eq "primary") {
  68.     $zname = shift(@rest);
  69.     &maybe_print_comment("","\n");
  70.     $new_config .= "zone \"$zname\" ";
  71.     if ($class ne "") {
  72.         $new_config .= "$class ";
  73.     }
  74.     $new_config .= "{\n";
  75.     $new_config .= "\ttype master;\n";
  76.     $filename = shift(@rest);
  77.     $new_config .= "\tfile \"$filename\";\n";
  78.     $new_config .= "};\n\n";
  79.     } elsif ($directive eq "secondary" || $directive eq "stub") {
  80.     if ($directive eq "secondary") {
  81.         $type = "slave";
  82.     } else {
  83.         $type = "stub";
  84.     }
  85.     $zname = shift(@rest);
  86.     &maybe_print_comment("","\n");
  87.     $new_config .= "zone \"$zname\" ";
  88.     if ($class ne "") {
  89.         $new_config .= "$class ";
  90.     }
  91.     $new_config .= "{\n";
  92.     $new_config .= "\ttype $type;\n";
  93.     $filename = pop(@rest);
  94.     if ($filename =~ /^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$/) {
  95.         push(@rest, $filename);
  96.         $filename = "";
  97.     } else {
  98.         $new_config .= "\tfile \"$filename\";\n";
  99.     }
  100.     $new_config .= "\tmasters {\n";
  101.     foreach $master (@rest) {
  102.         $new_config .= "\t\t$master;\n";
  103.     }
  104.     $new_config .= "\t};\n";
  105.     $new_config .= "};\n\n";
  106.     } elsif ($directive eq "cache") {
  107.         $zname = shift(@rest);
  108.     &maybe_print_comment("","\n");
  109.     $new_config .= "zone \"$zname\" {\n";
  110.     $new_config .= "\ttype hint;\n";
  111.     $filename = shift(@rest);
  112.     $new_config .= "\tfile \"$filename\";\n";
  113.     $new_config .= "};\n\n";
  114.     } elsif ($directive eq "directory") {
  115.     $options{"directory"} = "\"$rest[0]\"";
  116.     $options_comments{"directory"} = $comment;
  117.     $have_options = 1;
  118.     } elsif ($directive eq "check-names") {
  119.     $type = shift(@rest);
  120.     if ($type eq "primary") {
  121.         $type = "master";
  122.     } elsif ($type eq "secondary") {
  123.         $type = "slave";
  124.     }
  125.     $action = shift(@rest);
  126.     $options{"check-names $type"} = $action;
  127.     $options_comments{"check-names $type"} = $comment;
  128.     $have_options = 1;
  129.     } elsif ($directive eq "forwarders") {
  130.     $options{"forwarders"}="{\n";
  131.     foreach $forwarder (@rest) {
  132.         $options{"forwarders"} .= "\t\t$forwarder;\n";
  133.     }
  134.     $options{"forwarders"} .= "\t}";
  135.     $options_comments{"forwarders"} = $comment;
  136.     $have_options = 1;
  137.     } elsif ($directive eq "slave") {
  138.     &handle_options("forward-only");
  139.     } elsif ($directive eq "options") {
  140.     &handle_options(@rest);
  141.     } elsif ($directive eq "limit") {
  142.     &handle_limit(@rest);
  143.     } elsif ($directive eq "include") {
  144.     $new_config .= 
  145.         "// make sure your include is still in the right place\n";
  146.     $comment = "\t" . $comment;
  147.     $new_config .= "include \"$rest[0]\";$comment\n\n";
  148.     } elsif ($directive eq "xfrnets" || $directive eq "tcplist") {
  149.     if ($comment ne "") {
  150.         $comment = "\t$comment";
  151.     }
  152.     foreach $elt (@rest) {
  153.         push(@transfer_acl, $elt);
  154.         push(@transfer_comments, $comment);
  155.     }
  156.     $have_options = 1;
  157.     } elsif ($directive eq "sortlist") {
  158.     if ($comment ne "") {
  159.         $comment = "\t$comment";
  160.     }
  161.     foreach $elt (@rest) {
  162.         push(@topology, $elt);
  163.         push(@topology_comments, $comment);
  164.     }
  165.     } elsif ($directive eq "bogusns") {
  166.     if ($comment ne "") {
  167.         $comment = "\t$comment";
  168.     }
  169.     foreach $elt (@rest) {
  170.         push(@bogus, $elt);
  171.         push(@bogus_comments, $comment);
  172.     }
  173.     } elsif ($directive eq "max-fetch") {
  174.     $options{"transfers-in"}=$rest[0];
  175.     $options_comments{"transfers-in"}=$comment;
  176.     $have_options = 1;
  177.     } else {
  178.     $new_config .= "// NOTE: unconverted directive '$directive @rest'\n\n";
  179.     }
  180. }
  181.  
  182. print "// generated by named-bootconf.pl\n\n";
  183. if ($have_options) {
  184.     print "options {\n";
  185.     foreach $option (sort(keys(%options))) {
  186.     print "\t$option $options{$option};";
  187.     if ($options_comments{$option} ne "") {
  188.         print "\t$options_comments{$option}";
  189.     }
  190.     print "\n";
  191.     }
  192.     if (@transfer_acl > 0) {
  193.     print "\tallow-transfer {\n";
  194.     for ($i = 0; $i <= $#transfer_acl; $i++) {
  195.         &print_maybe_masked("\t\t", $transfer_acl[$i],
  196.                 $transfer_comments[$i]);
  197.     }
  198.     print "\t};\n";
  199.     }
  200.     print "\t/*
  201. \t * If there is a firewall between you and nameservers you want
  202. \t * to talk to, you might need to uncomment the query-source
  203. \t * directive below.  Previous versions of BIND always asked
  204. \t * questions using port 53, but BIND 8.1 uses an unprivileged
  205. \t * port by default.
  206. \t */
  207. \t// query-source address * port 53;
  208. ";
  209.  
  210.     print "};\n\n";
  211. }
  212. if ($logging ne "") {
  213.     print "logging {\n$logging};\n\n";
  214. }
  215. if (@topology > 0) {
  216.     print "// Note: the following will be supported in a future release.\n";
  217.     print "/*\n";
  218.     print "host { any; } {\n\ttopology {\n";
  219.     for ($i = 0; $i <= $#topology; $i++) {
  220.     &print_maybe_masked("\t\t", $topology[$i],
  221.                 $topology_comments[$i]);
  222.     }
  223.     print "\t};\n};\n";
  224.     print "*/\n";
  225.     print "\n";
  226. }
  227. if (@bogus > 0) {
  228.     for ($i = 0; $i <= $#bogus; $i++) {
  229.     print "server $bogus[$i] { bogus yes; };$bogus_comments[$i]\n";
  230.     }
  231.     print "\n";
  232. }
  233. print $new_config;
  234.  
  235. exit 0;
  236.  
  237. sub maybe_print_comment {
  238.     $prefix = shift;
  239.     $suffix = shift;
  240.     if ($comment ne "") {
  241.     $new_config .= sprintf("%s%s%s", $prefix, $comment, $suffix);
  242.     }
  243. }
  244.  
  245. sub handle_options {
  246.     foreach $option (@_) {
  247.     if ($option eq "forward-only") {
  248.         $options{"forward"}="only";
  249.         $options_comments{"forward"}=$comment;
  250.         $have_options = 1;
  251.     } elsif ($option eq "no-recursion") {
  252.         $options{"recursion"}="no";
  253.         $options_comments{"recursion"}=$comment;
  254.         $have_options = 1;
  255.     } elsif ($option eq "no-fetch-glue") {
  256.         $options{"fetch-glue"}="no";
  257.         $options_comments{"fetch-glue"}=$comment;
  258.         $have_options = 1;
  259.     } elsif ($option eq "fake-iquery") {
  260.         $options{"fake-iquery"}="yes";
  261.         $options_comments{"fake-iquery"}=$comment;
  262.         $have_options = 1;
  263.     } elsif ($option eq "query-log") {
  264.         if ($comment ne "") {
  265.         $logging .= "\t$comment\n";
  266.         }
  267.         $logging .= "\tcategory queries { default_syslog; };\n";
  268.     } else {
  269.         $options{"// NOTE: unconverted option '$option'"}="";
  270.         $options_comments{"// NOTE: unconverted option '$option'"}=
  271.         $comment;
  272.         $have_options = 1;
  273.     }
  274.     }
  275. }
  276.  
  277. sub handle_limit {
  278.     $limit = shift;
  279.     if ($limit eq "datasize" || $limit eq "transfers-in"
  280.     || $limit eq "transfers-per-ns" || $limit eq "files") {
  281.     $options{$limit}=$_[0];
  282.     $options_comments{$limit}=$comment;
  283.     $have_options = 1;
  284.     } else {
  285.     $options{"// NOTE: unconverted limit '$limit @_'"}="";
  286.     $options_comments{"// NOTE: unconverted limit '$limit @_'"}=$comment;
  287.     $have_options = 1;
  288.     }
  289. }
  290.  
  291. sub print_maybe_masked {
  292.     # this assumes a contiguous netmask starting at the MSB
  293.     $prefix = shift;
  294.     $elt = shift;
  295.     $elt_comment = shift;
  296.     if ($elt =~ /^(.*)&(.*)$/) {
  297.     $address = $1;
  298.     $mask = $2;
  299.     ($m1,$m2,$m3,$m4) = split(/\./, $mask);
  300.     $mask_val = ($m1 << 24) + ($m2 << 16) +($m3 << 8) + $m4;
  301.     $zero_bits = 0;
  302.     while (($mask_val % 2) == 0) {
  303.         $mask_val /= 2;
  304.         $zero_bits++;
  305.     }
  306.     $mask_bits = 32 - $zero_bits;
  307.     } else {
  308.     $address = $elt;
  309.     ($a1,$a2,$a3,$a4) = split(/\./, $address);
  310.     if ($a1 < 128) {
  311.         $mask_bits = 8;
  312.     } elsif ($a1 < 192) {
  313.         $mask_bits = 16;
  314.     } else {
  315.         $mask_bits = 24;
  316.     }
  317.     }
  318.     
  319.     print "$prefix$address";
  320.     if ($mask_bits != 32) {
  321.     print "/$mask_bits";
  322.     }
  323.     print ";$elt_comment\n";
  324. }
  325.